package proxyTest.serverProxy.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoop;
import io.netty.channel.SimpleChannelInboundHandler;
import proxyTest.serverProxy.client.Client2Server;

import java.util.concurrent.TimeUnit;

/**
 * @Author: jia
 * @Descirption:
 * @Date: 2018/5/16 14:06
 * @Modified By:
 */


public class ProxyServerHandler extends SimpleChannelInboundHandler {
    @Override
    protected void channelRead0(final ChannelHandlerContext ctx, Object o) throws Exception {
        System.out.println("代理服务器收到的消息==="+o);
        Channel channel = ctx.channel();
        EventLoop eventLoop = channel.eventLoop();
        //新建一个任务
        eventLoop.schedule(new Runnable() {
            @Override
            public void run() {
                ctx.channel().writeAndFlush("你好，我是代理服务器！");
            }
        },5, TimeUnit.SECONDS);
    }

    //当代理服务器和客户端建立连接时，
    //新建客户端连接到server
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        EventLoop eventLoop = ctx.channel().eventLoop();
        //连接到netty服务器
        Client2Server client2Server = new Client2Server("localhost",10102,eventLoop);
        client2Server.send("你好我是代理客户端，和你建立连接！");
        //连接到web服务器
    }
}
